package com.ufm.syslog.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ufm.syslog.pojo.Syslog;
import com.ufm.syslog.service.ISyslogService;
import com.ufm.util.PageUtil;

@Controller
public class SyslogController {
	@Autowired  
    private ISyslogService syslogService;
	
	@RequestMapping(value="/syslog/findAll", method=RequestMethod.GET)
	public String findAll(Model model) {
		List<Syslog> syslogs = syslogService.findAll(new HashMap());
		model.addAttribute("syslogs", syslogs);
		return "/login/syslog";
	}
	@RequestMapping(value="/syslog/deleteAll")
	public String deleteAll(@RequestParam("selIds") String selIds) {
		String[] ids = selIds.split(",");
		for (int i = 0; i < ids.length; i++) {
			syslogService.delete(Long.parseLong(ids[i]));
		}
		return "redirect:/syslog/findByDate";
	}
	
	@RequestMapping(value="/syslog/delete")
	public String delete() {
		syslogService.deleteAll();
		return "redirect:/syslog/findByDate";
	}
	
	@RequestMapping(value="/syslog/findByDate")
	public String findByDate(Model model,HttpServletRequest request) {
		Integer currentPageNo = PageUtil.PAGE_NUM;
		Integer perPageNumber = PageUtil.RECORD_NUM;	
		String currentPageNoStr = request.getParameter("currentPageNo");
		if(currentPageNoStr!=null && !"".equals(currentPageNoStr)){
			currentPageNo = Integer.parseInt(currentPageNoStr);
		}	
		String perPageNumberStr = request.getParameter("perPageNumber");
		if(perPageNumberStr!=null && !"".equals(perPageNumberStr)){
			perPageNumber = Integer.parseInt(perPageNumberStr);
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate=null;
		Date endDate=null;
		try {
			if(request.getParameter("startDate")!=null&&!"".equals(request.getParameter("startDate"))){
				startDate = sf.parse(request.getParameter("startDate"));
			}
			if(request.getParameter("endDate")!=null&&!"".equals(request.getParameter("endDate"))){
				endDate = sf.parse(request.getParameter("endDate"));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Map params = new HashMap();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		List<Syslog> syslogsTemp = syslogService.findAll(params);
		List<Syslog> syslogs = PageUtil.getPageList(syslogsTemp, currentPageNo, perPageNumber);
		int total = syslogsTemp.size();
		model.addAttribute("startDate",startDate);
		model.addAttribute("endDate",endDate);
		model.addAttribute("syslogs", syslogs);
		model.addAttribute("currentPageNo", currentPageNo);
		model.addAttribute("count", total == 0?1:total%perPageNumber == 0?total/perPageNumber:total/perPageNumber+1);
		return "/login/syslog";
	}
	/**
	 * 用于测试添加日志
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/syslog/insert",method=RequestMethod.GET)
	public String deleteOne(Model model) {
		Syslog syslog = new Syslog("登录", "登录", 1, 123456, "张三","E1-A2-33-Z4-R4", new Date());
		syslogService.insert(syslog);
		return "redirect:/syslog/findAll";
	}
	
}